package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
)

func Upload(file *multipart.FileHeader) (string, error) {

	accessKey := "40NEkayEBOzS7Sp5GGSfv7JUuSIEeXB_NHLBqSEN"
	secretKey := "4okQ4vBaYaEzTGp6WtShqdhMp9AAA19sVOlqyeWP"
	mac := credentials.NewCredentials(accessKey, secretKey)

	localFile := "http://sxd4qujl8.hn-bkt.clouddn.com/" + file.Filename
	bucket := "2302aluozheng"
	key := "http://sxd4qujl8.hn-bkt.clouddn.com/" + file.Filename

	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadFile(context.Background(), localFile, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	return key, err
}
